<template>
  <div>
    <button @click="handleUploadFile">上传文件</button>
    <input type="file" class="upload-file" style="display: none" />
  </div>
</template>
<script>
import ImageMagicAdapter from "image-magic-adapter";
export default {
  data() {
    return {};
  },
  methods: {
    handleUploadFile() {
      const uploadFileDom = document.querySelector(".upload-file");
      uploadFileDom.click();
      // 监听change
      uploadFileDom.addEventListener("change", async (e) => {
        const file = e.target.files[0];
        console.log(file);
        let ImageCompressorCls = ImageMagicAdapter.ImageCompressorCls;
        const imageCompressor = new ImageCompressorCls();
        try {
          let afterUrl = await imageCompressor.imgFormatFn(
            file,
            "application/pdf",
            true
          );
          console.log(afterUrl, "afterUrl");
        } catch (error) {
          console.log(error);
        }
      });
    },
  },
};
</script>
